package org.mbari.vcr.timer;

import org.mbari.util.IObserver;
import org.mbari.vcr.IVCR;
import org.mbari.vcr.IVCRState;
import org.mbari.vcr.VCRAdapter;

/**
 *
 * @author brian
 */
public class TimecodeMonitor extends Monitor {
    
    public static final String MONITOR_NAME = "TimecodeMonitor";

    private final IObserver stateObserver = new IObserver() {
        public void update(Object obj, Object changeCode) {
            IVCRState vcrState = (IVCRState) obj;
            if (vcrState != null && !vcrState.isStopped()) {
                //start();
                setInterval(40);
            }
            else {
                //stop();
                setInterval(250);
            }
        }
    };

    public TimecodeMonitor() {
        this(new VCRAdapter());
        setInterval(40);
    }

    public TimecodeMonitor(IVCR vcr) {
        super(ReadTimecodeTimerTask.class, MONITOR_NAME, vcr);
    }

    @Override
    public synchronized void setVcr(IVCR vcr) {
        IVCR oldVcr = getVcr();
        if (oldVcr != null) {
            oldVcr.getVcrState().removeObserver(stateObserver);
        }
        vcr.getVcrState().addObserver(stateObserver);
        super.setVcr(vcr);
        start();
    }
}
